﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Media;

namespace PingPong
{
    class PingPong
    {
        static Random randomGenerator = new Random();

        private static void MovePlayer1Pad(Player player)
        {
            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo keyPressed = Console.ReadKey(true);
                while (Console.KeyAvailable)
                {
                    Console.ReadKey(true);
                }
                if (keyPressed.Key == ConsoleKey.UpArrow)
                {
                    player.PlayerPad.MoveUp();
                }
                if (keyPressed.Key == ConsoleKey.DownArrow)
                {
                    player.PlayerPad.MoveDown();
                }
            }
        }
        /*
        private static void MovePlayer2Pad(Player player)
        {
            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo keyPressed = Console.ReadKey(true);
                while (Console.KeyAvailable)
                {
                    Console.ReadKey(true);
                }
                
                if (keyPressed.Key == ConsoleKey.LeftArrow)
                {
                    player.PlayerPad.MoveUp();
                }
                if (keyPressed.Key == ConsoleKey.RightArrow)
                {
                    player.PlayerPad.MoveDown();
                }
            }
        }*/
        // Edited by Nikola

        static void SecondPlayerAIMove(Ball ball, Player player)
        {
            int randomNumber = randomGenerator.Next(1, 100);

            if (randomNumber < 65)
            {
                if (ball.GoingLeft == false)
                {
                    if (ball.GoingUp == true)
                    {
                        player.PlayerPad.MoveUp();
                    }
                    else
                    {
                        player.PlayerPad.MoveDown();
                    }
                }
            }
        }

        /// <summary>
        /// Editet By Yordan
        /// </summary>
        public static void SoundsOnHits()
        {
            // written by Yordan little edits by Svetlin
            SoundPlayer player = new SoundPlayer();
            string fileNameSound = @"..//..//Sounds//pad hit even longer.wav";
            player.SoundLocation = fileNameSound;
            player.Play();
        }

        public static void SoundsOnBounces()
        {
            // written by Yordan little edits by Svetlin
            SoundPlayer player = new SoundPlayer();
            string fileNameSound = @"..//..//Sounds//boing6.wav";
            player.SoundLocation = fileNameSound;
            player.Play();
        }

        public static void SoundsOnLoss()
        {
            SoundPlayer player = new SoundPlayer();
            string fileNameSound = @"..//..//Sounds//boo.wav";
            player.SoundLocation = fileNameSound;
            player.Play();
        }

        public static void SoundsOnWin()
        {
            SoundPlayer player = new SoundPlayer();
            string fileNameSound = @"..//..//Sounds//applause.wav";
            player.SoundLocation = fileNameSound;
            player.Play();
        }

        public static void Theme()
        {
            SoundPlayer player = new SoundPlayer();
            string fileNameSound = @"..//..//Sounds//theme.wav";
            player.SoundLocation = fileNameSound;
            player.Play();
        }

        public static void StopTheme()
        {
            SoundPlayer playerToStop = new SoundPlayer();
            string fileNameSound = @"..//..//Sounds//theme.wav";
            playerToStop.SoundLocation = fileNameSound;
            playerToStop.Play();
            playerToStop.Stop();
        }

        static void Main(string[] args)
        {
            Console.CursorVisible = false;
            Draw draw = new Draw();

            PlayGround playGround = new PlayGround(100, 38);
            Draw.DrawStartScreen(playGround);

            Player player1 = new Player(Draw.AskForPlayerName());

            player1.PlayerPad.Position[0] = 1;
            player1.PlayerPad.Position[1] = playGround.Height / 2;

            Player aiPlayer = new Player("EntelegentenBoklik007");
            aiPlayer.PlayerPad.Position[0] = playGround.Width - 2;
            aiPlayer.PlayerPad.Position[1] = playGround.Height / 2;

            aiPlayer.Name = "Bot Sammy";

            Ball ball = new Ball('*', playGround);
            ball.ResetBall();

            List<Obstacle> obstacles = new List<Obstacle>();

            StopTheme();

            while (player1.Result != 3 && aiPlayer.Result != 3)
            {
                int numberOfObstacles = randomGenerator.Next(0, 3);
                for (int i = 0; i < numberOfObstacles; i++)
                {
                    if (randomGenerator.Next(0, 2) == 1)
                    {
                        obstacles.Add(new Obstacle(playGround));
                    }
                }
                Console.Clear();

                draw.DrawPlayGround(playGround);
                draw.DrawObstacle(obstacles);

                ball.MoveBall(player1, aiPlayer, obstacles);
                MovePlayer1Pad(player1);
                SecondPlayerAIMove(ball, aiPlayer);

                for (int i = 0; i < obstacles.Count; i++)
                {
                    if (obstacles[i].PositionY == playGround.Height - 3)
                    {
                        obstacles.RemoveAt(i);
                    }
                    obstacles[i].MoveObstacle(playGround);
                }

                draw.DrawBall(ball);
                draw.DrawPad(player1.PlayerPad);
                draw.DrawPad(aiPlayer.PlayerPad);

                Thread.Sleep(100);
            }
            Console.Clear();

            Console.WriteLine("The game has ended!");
            if (player1.Result == 3)
            {
                Console.WriteLine("The winner is {0} with {1} - {2}", player1.Name, player1.Result, aiPlayer.Result);
            }
            else if (aiPlayer.Result == 3)
            {
                Console.WriteLine("The winner is {0} with {1} - {2}", aiPlayer.Name, aiPlayer.Result, player1.Result);
            }

            DateTime now = DateTime.Now;
            PlayGround.SaveScore(player1, aiPlayer, now);
        }
    }
}